home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / DOUBLEDE / DEMOSTUF.C < prev    next >
C/C++ Source or Header  |  1988-07-28  |  3KB  |  156 lines

  1. #include "shell.h"
  2.  
  3. posbutton(which)
  4. int        which;
  5. {
  6.     Rect        r;
  7.     
  8.     r = windows[which]->portRect;
  9.     r.right -= SCROLLBARWIDTH;
  10.     r.bottom -= 30;
  11.     r.top = r.bottom -20;
  12.     
  13.     InsetRect(&r,(r.right-r.left-80)/2,0);
  14.     
  15.     HideControl(demoButton[which]);
  16.     MoveControl(demoButton[which],r.left,r.top);
  17.     SizeControl(demoButton[which],80,20);
  18.     ShowControl(demoButton[which]);
  19. }
  20.  
  21. do_demo(which)
  22. int        which;
  23. {
  24.     DialogTHndl        dlgTmpl;
  25.     DialogPtr        theDlg;
  26.     Handle            dHandle;
  27.     int                dType;
  28.     Rect            dRect;
  29.     int                itemHit;
  30.     Str255            utilStr;
  31.     GrafPtr            oldPort;
  32.     
  33.     Boolean            validResult = TRUE;
  34.  
  35.     Decimal            _decimal_;
  36.     double            argument;
  37.     Boolean            read_err;
  38.     int                index;
  39.     
  40.     dlgTmpl = (DialogTHndl)GetResource('DLOG',6001);
  41.     if (dlgTmpl){
  42.         /*
  43.             center the dialog in the screen rectangle
  44.         */
  45.         HNoPurge(dlgTmpl);
  46.         centerrect(&(**dlgTmpl).boundsRect,&screen_rect);
  47.         
  48.         /*
  49.             get the dialog from the resource fork
  50.         */
  51.         theDlg = GetNewDialog(6001,NIL,-1L);
  52.         
  53.         /*
  54.             convert the double to a C string, and then to a pascal
  55.             string to place in the dialog
  56.         */
  57.         double_2_cString(&demo_double[which],FIXEDDECIMAL,6,utilStr);
  58.         CtoPstr((char *)utilStr);
  59.         
  60.         /*
  61.             set dialog item 4 (the editable text) to the string
  62.             converted above, and then select it
  63.         */
  64.         GetDItem(theDlg,4,&dType,&dHandle,&dRect);
  65.         SetIText(dHandle,utilStr);
  66.         SelIText(theDlg,4,0,32000);
  67.         
  68.         ShowWindow(theDlg);
  69.         
  70.         do{    /* until we get a valid double result */
  71.  
  72.             do
  73.                 ModalDialog(NIL,&itemHit);
  74.             while(itemHit > 2);
  75.             
  76.             if (itemHit == OK){
  77.                 /*
  78.                     get the resultant string from item 4 of the
  79.                     dialog
  80.                 */
  81.                 GetDItem(theDlg,4,&dType,&dHandle,&dRect);
  82.                 GetIText(dHandle,utilStr);
  83.                 PtoCstr((char *)utilStr);
  84.                 
  85.                 /*
  86.                     use SANE to convert it to a decimal record
  87.                 */
  88.                 index = 0;
  89.                 CStr2Dec(utilStr,&index,&_decimal_,&read_err);
  90.                 if (read_err){
  91.                     
  92.                     /*
  93.                         if no conversion error,
  94.                         convert the decimal record to a double
  95.                     */
  96.                     fp68k(&_decimal_,&argument,FFEXT|FOD2B);
  97.                                         
  98.                     /*
  99.                         set the global double to the double
  100.                         we got from the dialog
  101.                     */
  102.                     demo_double[which] = argument;
  103.                     
  104.                     /*
  105.                         force an update of the application window
  106.                     */
  107.                     GetPort(&oldPort);
  108.                     SetPort(windows[which]);
  109.                     InvalRect(&windows[which]->portRect);
  110.                     SetPort(oldPort);
  111.                     
  112.                     /*
  113.                         allow us out of dialog
  114.                     */
  115.                     validResult = TRUE;
  116.                 }
  117.                 else{
  118.                     /*
  119.                         we couldn't convert the resultant string,
  120.                         so beep and reselect the text.
  121.                     */
  122.                     validResult = FALSE;
  123.                     SysBeep(1);
  124.                     SelIText(theDlg,4,0,32000);
  125.                 }
  126.             }
  127.         
  128.         }while(!validResult);
  129.         
  130.         DisposDialog(theDlg);
  131.         HPurge(dlgTmpl);
  132.     }
  133. }
  134.  
  135. char *double_2_cString(value, sane_style, dec_digits, result)
  136. double        *value;
  137. char        sane_style;
  138. int            dec_digits;
  139. char         *result;
  140. {
  141.     DecForm        aDecForm;
  142.     Decimal     aDecimal;
  143.  
  144.     aDecForm.style = sane_style;
  145.     aDecForm.digits = dec_digits;
  146.  
  147.     fp68k(&aDecForm, value, &aDecimal, FFEXT+FOB2D);
  148.  
  149.     aDecForm.style = sane_style;
  150.     aDecForm.digits = dec_digits;
  151.  
  152.     Dec2Str(aDecForm, &aDecimal,result);
  153.  
  154.     return (PtoCstr(result));
  155. }
  156.